package org.jeecg.modules.lock.service.impl;


import org.jeecg.modules.basic.electronic.service.ILwgyInoutElectronicService;
import org.jeecg.modules.lock.service.IOpenCloseElectricityService;
import org.jeecg.modules.lock.service.LwgyDoorLockService;
import org.jeecg.modules.lock.util.wsCallServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

import static org.jeecg.modules.basic.entry.util.DkUtils.DLDkURL;

/**
 * 房间电控Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-12
 */
@Service
public class OpenCloseElectricityServiceImpl implements IOpenCloseElectricityService {

    @Autowired
    private ILwgyInoutElectronicService iILwgyInoutElectronicService;

    @Autowired
    private LwgyDoorLockService lwgyDoorLockService;


    @Override
    public String useSetSBKGData(String xq, String fjh, String lx, String xl) {
        System.out.println("开始调用电控接口..........");
        String result = "";
        Map params = new HashMap<String, String>(16);
        params.clear();
        try {
            params.put("inMm", "60C85273A0D8834B798C5FA766D76B866B609AA755C3584F797F798C7FF8785D");
            params.put("inXq", xq);
            params.put("inFj", fjh);
            params.put("inLx", lx);
            params.put("inXl", xl);

            if(DLDkURL.length()< 5){
                DLDkURL = iILwgyInoutElectronicService.getInterfaceAddressGl();
            }
            System.out.println("DLDkURL ==========: " +DLDkURL);
            result = wsCallServices.callServiceHC(DLDkURL, xq,fjh,lx,xl);
            System.out.println("result -------- " + result);
            String setResult = parseSetSBKGDataResult(result);
            System.out.println("setResult -------- " + setResult);
            return  setResult;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            return "操作失败";
        }
    }

    public String parseSetSBKGDataResult(String xmlString) {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(new InputSource(new StringReader(xmlString)));

            NodeList nodeList = doc.getElementsByTagName("SetSBKGDataResult");
            if (nodeList.getLength() > 0) {
                Element element = (Element) nodeList.item(0);
                return element.getTextContent();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
